package com.blb.community.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * @Author 帅帅
 * @Date 2022/7/12 15:03
 * @PackageName:com.blb.community.config
 * @ClassName: SexConverter
 * @Description: EasyExcel男女处理
 * @Version 1.0
 */
public class SexConverter implements Converter<Integer> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Converter.super.supportJavaTypeKey();
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return Converter.super.supportExcelTypeKey();
    }

    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) throws Exception {
        return Converter.super.convertToJavaData(context);
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) throws Exception {
        return Converter.super.convertToExcelData(context);
    }

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if ("男".equals(cellData.getStringValue())) {
            return 0;
        }
        if ("女".equals(cellData.getStringValue())) {
            return 1;
        }
        return 2;
    }


    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {

        if (value.equals(0)) {
         return    new WriteCellData<>("男");
        }
        if (value.equals(1)) {
         return    new WriteCellData<>("女");
        }
        return new WriteCellData<>("未知");
    }


}
